var gcoord = require('../../../../utils/gcoord.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    txt: 'yes',
    popshow: true,
    applyFor: false,
    querylist: [{
        query: "后方可是否合适附近的爽肤水",
        answer: "回复框架的师傅的说法后第三方返回数据库",
        open: true
      },
      {
        query: "后方可是否合适附近的爽",
        answer: "回复框架的师傅的说法后第三方返回数据库",
        open: false
      },
      {
        query: "后方可是否合适附近的爽肤水",
        answer: "回复框架的师傅的说法后第三方返回数据库",
        open: false
      },
      {
        query: "后方可是否合适附近的爽肤",
        answer: "回复框架的师傅的说法后第三方返回数据库",
        open: false
      },
      {
        query: "后方可是否合适附近的爽",
        answer: "回复框架的师傅的说法后第三方返回数据库",
        open: false
      },
      {
        query: "后方可是否合适附近的爽肤",
        answer: "回复框架的师傅的说法后第三方返回数据库",
        open: false
      },
    ],
    type: 0,
  },
  // 
  bindtype(e) {
    var that = this
    that.setData({
      type: e.currentTarget.dataset.index,
      mode: e.currentTarget.dataset.index,
    })
  },
  quitclick() {
    this.setData({
      popshow: false,
      applyFor: false
    })
  },
  // 调取申请
  ptiontitbtn() {
    this.setData({
      applyFor: true,
      txt: 'none',
      popshow: false
    })
  },
  openitem(e) {
    let i = e.currentTarget.dataset.i,
      arr = this.data.querylist
    arr.map(function (list) {
      list.open = false
    })
    arr[i].open = true
    this.setData({
      querylist: arr
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this

    var xufang = options.xufang
    var arr1 = wx.getStorageSync("Address") || [];
    if (decodeURIComponent(options.scene) !== 'undefined') {
      var res = decodeURIComponent(options.scene);
      var id = res.split("id=")[1];
      //查询是否有权限
      //扫码核销
      app.util.request({
        'url': 'entry/wxapp/goods.upgoodstatus',
        data: {
          id: id
        },
        success: function (res) {
          console.log(res)
        }
      })
      that.setData({
        id: id
      })
    } else {
      var id = options.id
      that.setData({
        id: id,
        xufang: xufang,
        addresslist: arr1,
        addressid: arr1.addressId
      })
      that.getontorder()
      var bgc = wx.getStorageSync('color')
      wx.setNavigationBarColor({
        frontColor: "#ffffff",
        backgroundColor: bgc,
      })
    }
  },
  chakwliu: function (e) {
    var that = this
    var orderunique = e.currentTarget.dataset.orderunique
    var id = e.currentTarget.dataset.id
    var orderss = e.currentTarget.dataset.orderss
    var orderstatus = e.currentTarget.dataset.orderstatus
    //查询默认物流公司
    app.util.request({
      'url': 'entry/wxapp/index.wuliucheack',
      data: {},
      success: function (res) {
        console.log(res)
        var name = res.data.name
        var com = res.data.com
        wx.navigateTo({
          url: '/hyb_yl/userCommunicate/pages/load/load?gsname=' + name + '&orderss=' + orderss + '&id=' + id + '&com=' + com + '&num=' + orderunique + '&orderstatus=' + orderstatus,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  getontorder() {
    var that = this
    var id = that.data.id
    //var ifpay = that.data.id
    app.util.request({
      'url': 'entry/wxapp/chufang.doPgedetail',
      data: {
        id: id,
        //ifpay: ifpay,
      },
      success: function (res) {
        console.log(res)
        if (res.data.goodstype == 1 && (res.data.orderStatus == 0 || res.data.orderStatus == 1 || res.data.orderStatus == 2)) {
          that.setData({
            type: 1,
            mode: 1
          })
        } else {
          that.setData({
            type: 0,
            mode: 0
          })
        }
        that.setData({
          info: res.data,
          mode: res.data.mode,
          goodstype: res.data.goodstype,

        })
      }
    })
  },
  copy: function (e) {
    var t = e.currentTarget.dataset.orders;
    wx.setClipboardData({
      data: t,
      success: function (t) {
        wx.getClipboardData({
          success: function () {
            wx.showToast({
              title: "复制成功",
              icon: "none",
              duration: 2e3
            });
          }
        });
      }
    });
  },
  // 选择微信收货地址
  address: function () {
    console.log(1)
    var region = [];
    var arr1 = [];
    var that = this
    wx.chooseAddress({
      success: function (res) {
        console.log("res")
        console.log(res)
        console.log(JSON.stringify(res))
        var name = res.userName
        var phone = res.telNumber
        region.push(res.provinceName, res.cityName, res.countyName)
        var address = res.detailInfo
        var addlist = {
          name: res.userName,
          phone: res.telNumber,
          address: res.detailInfo,
          region: region,
          label: -1,
          addlabel: ""
        }
        console.log(addlist)
        //添加收货人信息
        var userName = addlist.name
        var userPhone = addlist.phone
        var userAddress = addlist.region[0] + '-' + addlist.region[1] + '-' + addlist.region[2] + '-' + addlist.address
        app.util.request({
          'url': 'entry/wxapp/goods.creatuseraddress',
          data: {
            openid: wx.getStorageSync('openid'),
            userName: userName,
            userPhone: userPhone,
            userAddress: userAddress
          },
          success: function (res) {

            var addressid = res.data.addressId
            console.log(addressid)
            wx.setStorageSync("Address", res.data)
            that.setData({
              addressid: addressid,
              addresslist: res.data
            })
            if (res.data == "") {
              that.setData({
                addbox: true
              })
            } else {
              that.setData({
                addbox: false
              })
            }
          }
        })
        arr1 = wx.getStorageSync("Address") || [];
        that.setData({
          addresslist: arr1
        })
      },
      fail: function (err) {
        that.openConfirm()


      }
    })
  },
  changeStatuspay(e) {
    var that = this
    var id = e.currentTarget.dataset.id
    var orders = e.currentTarget.dataset.orders
    var money = parseFloat(e.currentTarget.dataset.money)
    var addressid = that.data.addressid
    var tkid = app.globalData.tkid //一级
    var mytkid = app.globalData.mytkid //二级
    console.log(tkid, mytkid)
    if (that.data.xufang == '1') {
      //续方增加新的药品订单
      app.util.request({
        'url': 'entry/wxapp/chufang.xufang',
        data: {
          id: id,
          addressid: addressid,
          openid: wx.getStorageSync('openid')
        },
        success: function (res) {
          console.log(res)
          var orderNo = res.data
          app.util.request({
            'url': 'entry/wxapp/yuyue.paycforder',
            header: {
              "Content-Type": "application/xml"
            },
            method: "GET",
            data: {
              openid: wx.getStorageSync('openid'),
              z_tw_money: money,
              orders: orders
            },
            success: function (res) {
              console.log(res)
              wx.requestPayment({
                'timeStamp': res.data.timeStamp,
                'nonceStr': res.data.nonceStr, //字符串随机数
                'package': res.data.package,
                'signType': res.data.signType,
                'paySign': res.data.paySign,
                success: function (res) {
                  console.log(res)
                  //更新订单信息
                  app.util.request({
                    'url': 'entry/wxapp/yuyue.uporderyao',
                    data: {
                      orders: orders
                    },
                    success: function (res) {
                      console.log(res)
                    }
                  })
                  app.util.request({
                    'url': 'entry/wxapp/tuike.uptopmoney',
                    data: {
                      tkid: tkid,
                      mytkid: mytkid,
                      money: money,
                      openid: wx.getStorageSync('openid'),
                      orders: orders,
                      leixing: 'yuanchengkaifang'
                    },
                    success: function (res) {
                      console.log(res)
                    }
                  })
                  wx.reLaunch({
                    url: '/hyb_yl/userCommunicate/pages/order/order',
                  })
                },
                fail: function (res) {
                  wx.reLaunch({
                    url: '/hyb_yl/userCommunicate/pages/order/order',
                  })
                }
              })
            }
          })
        }
      })

    } else {
      //直接购买
      app.util.request({
        'url': 'entry/wxapp/yuyue.paycforder',
        header: {
          "Content-Type": "application/xml"
        },
        method: "GET",
        data: {
          openid: wx.getStorageSync('openid'),
          z_tw_money: money,
          orders: orders
        },
        success: function (res) {
          console.log(res)
          wx.requestPayment({
            'timeStamp': res.data.timeStamp,
            'nonceStr': res.data.nonceStr, //字符串随机数
            'package': res.data.package,
            'signType': res.data.signType,
            'paySign': res.data.paySign,
            success: function (res) {
              //更新订单信息
              that.getontorder()
              app.util.request({
                'url': 'entry/wxapp/yuyue.uporderyao',
                data: {
                  orders: orders,
                  mode: that.data.mode
                },
                success: function (res) {
                  console.log(res)
                }
              })
              wx.reLaunch({
                url: '/hyb_yl/userCommunicate/pages/order/order',
              })
            }
          })
        }
      })
    }

  },
  daohang(e) {
    var latitude = parseFloat(e.currentTarget.dataset.latitude)
    var longitude = parseFloat(e.currentTarget.dataset.longitude)
    var result = gcoord.transform(
      [longitude, latitude], // 经纬度坐标
      gcoord.BD09, // 当前坐标系（百度）
      gcoord.GCJ02);

    wx.openLocation({
      latitude: result[1],
      longitude: result[0],
      address: e.currentTarget.dataset.address,
      scale: 18
    })

  }
})